/*
 * Designed and developed by 2024 androidpoet (Ranbir Singh)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.wsl.compose.chart.gant

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import kotlin.math.max

class GanttChartRenderer(
     val data: GanttChartData,
) {
  fun calculateMaxValues(): Pair<Float, Float> {
    val maxMonth = data.tasks.maxOfOrNull { it.startMonth + it.duration } ?: 1f
    return Pair(max(maxMonth, 1f), max(data.tasks.size.toFloat(), 1f))
  }

  fun drawTasks(
    drawScope: DrawScope,
    chartLeft: Float,
    chartTop: Float,
    chartWidth: Float,
    chartHeight: Float,
    maxMonth: Float,
    animationProgress: Float,
  ) {
    if (data.tasks.isEmpty()) return
    val safeMaxMonth = max(maxMonth, 1f)
    val taskHeight = max(chartHeight / data.tasks.size, 1f)

    data.tasks.forEachIndexed { index, task ->
      val startX = chartLeft + (task.startMonth / safeMaxMonth) * chartWidth
      val width = max((task.duration / safeMaxMonth) * chartWidth * animationProgress, 1f)
      val y = chartTop + index * taskHeight
      val color =
        if (index < data.taskColors.size) {
          data.taskColors[index]
        } else {
          Color.Blue
        }
      drawScope.drawRect(
        color = color.copy(alpha = animationProgress),
        topLeft = Offset(startX, y + taskHeight * 0.1f),
        size = Size(width, max(taskHeight * 0.8f, 1f)),
      )
    }
  }
}
